Building a Category Page
In this article we'll show you how to build a category page using best practices.
Getting the current category
First of all, you'll need the category the visitor is actually looking at. To obtain this, you can use the following code.
Ucommerce.Api.ICatalogContext catalogContext = Ucommerce.Infrastructure.ObjectFactory.Instance.Resolve<ICatalogContext>(); Ucommerce.Search.Models.Category currentCategory = catalogContext.CurrentCategory;
Once you have obtained the category in context you have multiple options for presenting the data available. Typically you'd like to present some category data like an image, display name and maybe a description.
All information is available in the language automatically.
string image = currentCategory.ImageMediaUrl; string displayName = currentCategory.DisplayName; string description = currentCategory.Description;
Dynamic properties
If you are interested in mapping and showing dynamic properties from the definition on your category, these can be retrieved via:
var myDynamicProperty = currentCategory["fieldName"];
Product Listing
It is likely that you'll need to list products in your category. For this purpose you can use the following code:
Ucommerce.Api.ICatalogLibrary catalogLibrary = Ucommerce.Infrastructure.ObjectFactory.Instance.Resolve<Ucommerce.Api.ICatalogLibrary>(); //if you pass in null for categoryId it will use category in context. ResultSet<Product> products = catalogLibrary.GetProducts(categoryId: null, skip: 0, take: 300);
Prices for products
The products retrieved from the API will contain the list prices as specified in the backend e.g. without any discounts or VAT applied.
These are accessed via:
var currentPriceGroup = catalogContext.CurrentPriceGroup; foreach (var product in products) { var unitPrice = new Money(product.UnitPrices[currentPriceGroup.Name] * (1.0M + currentPriceGroup.TaxRate), currentPriceGroup.CurrencyISOCode).ToString(); var withoutVat = new Money(product.UnitPrices[currentPriceGroup.Name], currentPriceGroup.CurrencyISOCode).ToString(); var vat = new Money(product.UnitPrices[currentPriceGroup.Name] * currentPriceGroup.TaxRate, currentPriceGroup.CurrencyISOCode).ToString(); }
The code above gets the price in the current price group and applies VAT. These prices do not include discounts which means the customer will potentially get another price.
If you want to do price calculations to show VAT and discounts this can be done using the following:
//price will include discounts as evaluated by marketing. Expensive operation so mind the amount of data and how you calculate it. Ucommerce.Catalog.Models.ProductPriceCalculationResult prices = catalogLibrary.CalculatePrices(products.Select(x => x.Guid).ToList());
Please note based on the amount of data you wish to retrieve that this operation is expensive.
URLs for products
When you wish to navigate to your products the following snippet gives you the URL for your products.
foreach (Product product in products) { IUrlService urlService = ObjectFactory.Instance.Resolve<Ucommerce.Search.Slugs.IUrlService>(); string url = urlService.GetUrl(catalogContext.CurrentCatalog, catalogContext.CurrentCategories.Append(catalogContext.CurrentCategory).Compact(), product); }